package com.basic.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.basic.common.result.Errors;
import com.basic.common.result.WebResult;
import com.basic.sys.domain.HisDataBo;
import com.basic.sys.domain.HisDataResult;
import com.basic.sys.domain.SelHisDataBo;
import com.basic.sys.mapper.HisDataExMapper;
import com.basic.sys.service.HisDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HisDataServiceImpl implements HisDataService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    HisDataExMapper hisDataExMapper;


    @Override
    public List<HisDataBo> selHisDataList(SelHisDataBo sel) {
        return null;
    }

    @Override
    public WebResult selAllHisDataList(SelHisDataBo sel) {
        if(sel == null || sel.getId() == null){
            logger.info("----------查询全部历史数据参数缺失---------");
            return null;
        }
        Integer pageNum = 1;
        sel.setPageNumber(pageNum);
        if(sel.getPageNum() !=null){
            pageNum = sel.getPageNum();
            sel.setPageNumber(pageNum);
        }
        //分页数 取appliction.proerties 配置文件中
        Integer pageSize = 10;
        sel.setPageSize(pageSize);
        if(sel.getPageSize() !=null){
            pageSize = sel.getPageSize();
            sel.setPageSize(pageSize);
        }
        List<String> tableNameList =new ArrayList<>();
        //默认年份
        if(Strings.isBlank(sel.getStartDate()) && Strings.isBlank(sel.getEndDate())){
            Year currentYear = Year.now();
            int year = currentYear.getValue();
            sel.setYear(String.valueOf(year));
            logger.info("Current Year: " + year);
            sel.setTableName("s"+sel.getId()+"_"+sel.getYear());
            tableNameList.add("s"+sel.getId()+"_"+sel.getYear());
            sel.setTableNameList(tableNameList);
        }else if(Strings.isNotBlank(sel.getStartDate()) && Strings.isNotBlank(sel.getEndDate())){
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy");
            try {
                Date startDate = outputFormat.parse(sel.getStartDate());
                Date endDate = outputFormat.parse(sel.getEndDate());
                String start = outputFormat.format(startDate);
                String end = outputFormat.format(endDate);
                int startFor = Integer.valueOf(start);
                int endFor = Integer.valueOf(end);
//                sel.setTableName("s"+sel.getId()+"_"+start);
                logger.info("---提取的开始年份是: " + start);
                logger.info("---提取的结束年份是: " + end);
                for (int i = startFor; i <= endFor; i++) {
                    tableNameList.add("s"+sel.getId()+"_"+i);
                }
                sel.setTableNameList(tableNameList);

            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        if(Strings.isNotBlank(sel.getSelVal()) && sel.getInterVal() !=null){
            String selValStr ="";
            String[] selVal = sel.getSelVal().split(",");
            for(int i=0;i<selVal.length;i++){
                if (i == 0) {
                    selValStr = selValStr + " min(" + selVal[i] + ")" + " as " + selVal[i];
                } else {
                    selValStr = selValStr + ", min(" + selVal[i] + ")" + " as " + selVal[i];
                }
            }
            sel.setSelValStr(selValStr);
        }
        WebResult result1 = new WebResult();
        List<HisDataBo> list = new ArrayList<>();
        Long isHave = hisDataExMapper.selIsHavaTable(sel);
        if (isHave == 0) {
            logger.info("----------查询表名不存在---------" + JSON.toJSONString(tableNameList));
            result1.setCode("1");
            result1.setMsg("当前泵站无数据");
            result1.setException(Errors.SYSTEM_REQUEST_PARAM_ERROR.exception);
            return result1;
        }
        Long count =0l;

        if("ALL".equals(sel.getSelType())){
            list = hisDataExMapper.selAllHisDataList(sel);
            //总页数
            count  = hisDataExMapper.selHisDataCount(sel);
        }else {
            list = hisDataExMapper.selHisDataListByVal(sel);
            //总页数
            count  = hisDataExMapper.selHisDataCountByVal(sel);
        }
        HisDataResult result = new HisDataResult();
        if(list !=null && list.size()>0){
            result.setList(list);
            result.setCount(count);
        }else {
            result.setCount(0L);
            result.setList(null);
        }
        result1.setData(result);
        return result1;
//        if(list ==null || list.size()==0){
//            PageInfo<HisDataBo> pageInfo = new PageInfo<HisDataBo>();
//            return pageInfo;
//        }

//        PageInfo<HisDataBo> pageInfo = new PageInfo<HisDataBo>(list);
//        return pageInfo;
    }
}
